/* eslint-disable */
import { inputData } from "./cwbbData.js";
import { fixZeroInfo } from '../../lib/zeroReportUtils.js';
let currentZeroInfo;
function nextTick(findObj, tick) {
  function doIt() {
    var obj = findObj();
    if (!obj) {
      setTimeout(() => {
        nextTick(findObj);
      }, 200);
    }
  }
  if (tick) {
    setTimeout(doIt, tick);
  } else {
    doIt();
  }
}
//
const cwbbIsReport = () => {
  return !Boolean(window.sessionStorage.getItem("isCwbb"));
};
// 判断财务报表是否报税
const cwbbReportStart = async (currentZeroInfo) => {
  currentZeroInfo = currentZeroInfo;
  let cwbbType = ["企业会计准则", "小企业会计准则"];
  nextTick(() => {
    if (
      $("#ifrMain").length &&
      $("#ifrMain").contents().find("#lhsbIframe").length &&
      $("#ifrMain")
        .contents()
        .find("#lhsbIframe")
        .contents()
        .find("#viewCtrlid > div.searchbox:eq(1) tr.ng-scope").length
    ) {
      let lis = $("#ifrMain")
        .contents()
        .find("#lhsbIframe")
        .contents()
        .find("#viewCtrlid > div.searchbox:eq(1) tr.ng-scope");
      let cwbbLi = Array.from(lis).find((item) => {
        let kjzd = $(item).find("td:eq(1)").text();
        let kssj = $(item).find("td:eq(3)").text().split("-");
        let jssj = $(item).find("td:eq(4)").text().split("-");
        if (
          cwbbType.includes(kjzd) &&
          parseInt(kssj[0]) === parseInt(currentZeroInfo.year) &&
          parseInt(kssj[1]) === parseInt(currentZeroInfo.beginPeriod) &&
          parseInt(jssj[1]) === parseInt(currentZeroInfo.endPeriod)
        ) {
          return item;
        }
      });
      if (cwbbLi) {
        let href = $(cwbbLi).find("a").attr("href").replace("javaScript:", "");
        $("#ifrMain")
          .contents()
          .find("#lhsbIframe")[0]
          .contentWindow.eval(href);
        nextTick(() => {
          window.postMessage(
            {
              event: "closeTabs",
            },
            "*"
          );
          return true;
        }, 1000);
      } else {
        window.postMessage(
          {
            event: "zeroReportFail",
            data: {
              success: false,
              message: "会计准则或者申报周期错误",
            },
          },
          "*"
        );
      }
      return true;
    }
  });
};
const transformData = (cwbbInfo, inputDatas, type, insType) => {
  if (type === "zcfzb") {
    let zcfzb = cwbbInfo.zcfzb;
    return inputDatas.map((item) => {
      if (item.hasOwnProperty("qmyeZc")) {
        item.qmyeZc =
          item.qmyeZc && zcfzb.hasOwnProperty(item.qmyeZc)
            ? zcfzb[item.qmyeZc].qmye
            : "0.00";
      }
      if (item.hasOwnProperty("ncyeZc")) {
        item.ncyeZc =
          item.ncyeZc && zcfzb.hasOwnProperty(item.ncyeZc)
            ? zcfzb[item.ncyeZc].ncye
            : "0.00";
      }
      if (item.hasOwnProperty("qmyeQy")) {
        item.qmyeQy =
          item.qmyeQy && zcfzb.hasOwnProperty(item.qmyeQy)
            ? zcfzb[item.qmyeQy].qmye
            : "0.00";
      }
      if (item.hasOwnProperty("ncyeQy")) {
        item.ncyeQy =
          item.ncyeQy && zcfzb.hasOwnProperty(item.ncyeQy)
            ? zcfzb[item.ncyeQy].ncye
            : "0.00";
      }
      return item;
    });
  }
  if (type === "lrb") {
    let lrb = cwbbInfo.lrb;
    return inputDatas.map((item) => {
      if (insType === "xqykjzz") {
        item.bnljje =
          item.bnljje && lrb.hasOwnProperty(item.bnljje)
            ? lrb[item.bnljje].bnljje
            : "0.00";
        item.byje =
          item.byje && lrb.hasOwnProperty(item.byje)
            ? lrb[item.byje].bqje
            : "0.00";
      } else {
        item.sqje1 =
          item.sqje1 && lrb.hasOwnProperty(item.sqje1)
            ? lrb[item.sqje1].sqje
            : "0.00";
        item.bqje =
          item.bqje && lrb.hasOwnProperty(item.bqje)
            ? lrb[item.bqje].bqje
            : "0.00";
      }
      return item;
    });
  }
  if (type === "xjllb") {
    let xjllb = cwbbInfo.xjllb;
    return inputDatas.map((item) => {
      if (insType === "xqykjzz") {
        item.bnljje =
          item.bnljje && xjllb.hasOwnProperty(item.bnljje)
            ? xjllb[item.bnljje].bnljje
            : "0.00";
        item.byje =
          item.byje && xjllb.hasOwnProperty(item.byje)
            ? xjllb[item.byje].bqje
            : "0.00";
      } else {
        item.sqje1 =
          item.sqje1 && xjllb.hasOwnProperty(item.sqje1)
            ? xjllb[item.sqje1].sqje
            : "0.00";
        item.bqje =
          item.bqje && xjllb.hasOwnProperty(item.bqje)
            ? xjllb[item.bqje].bqje
            : "0.00";
      }
      return item;
    });
  }
};
const handleStep10 = (data) => {
  currentZeroInfo = data;
  nextTick(() => {
    if (
      $("#iframehtm").length &&
      $("#iframehtm")
        .contents()
        .find(
          "#viewCtrlId > div:nth-child(7) > div.divhide > div > table > tbody > tr:nth-child(1) > td:nth-child(2) > span:nth-child(1) > select"
        ).length
    ) {
      let val =
        $("#iframehtm")
          .contents()
          .find(
            "#viewCtrlId > div:nth-child(7) > div.divhide > div > table > tbody > tr:nth-child(1) > td:nth-child(2) > span:nth-child(1) > select"
          )
          .val() + "";
      if (["101", "102"].includes(val)) {
        $("#save").click();
        nextTick(() => {
          if ($("#layui-layer1 > div.layui-layer-btn.layui-layer-btn- > a"))
            $(
              "#layui-layer1 > div.layui-layer-btn.layui-layer-btn- > a"
            ).click();
          return true;
        }, 1000);
        return true;
      }
      if (data.companyType === 1) {
        // 企业会计准则
        $("#iframehtm").contents().find("#zlbszlDm").val("ZL100100101");
        $("#iframehtm")
          .contents()
          .find(
            "#viewCtrlId > div:nth-child(7) > div.divhide > div > table > tbody > tr:nth-child(1) > td:nth-child(2) > span:nth-child(1) > select"
          )
          .val("101");
      }
      if (data.companyType === 3) {
        // 小企业会计准则
        $("#iframehtm").contents().find("#zlbszlDm").val("ZL1001003");
        $("#iframehtm")
          .contents()
          .find(
            "#viewCtrlId > div:nth-child(7) > div.divhide > div > table > tbody > tr:nth-child(1) > td:nth-child(2) > span:nth-child(1) > select"
          )
          .val("102");
      }
      return true;
    }
  }, 500);
};
const xjllbReport = async (optionBtn, type) => {
  return new Promise((resolve) => {
    nextTick(() => {
      let btn = $("#layui-layer4 > div.layui-layer-btn.layui-layer-btn- > a");
      if (btn.length) btn.click();
      // 现金流量填写
      optionBtn.querySelector("a").click();
      let tableDetail = $("#frmMain").contents().find("#frmSheet")[0];
      tableDetail.onload = () => {
        nextTick(() => {
          console.log(currentZeroInfo);
          let insType =
            currentZeroInfo.companyType === 1 ? "qykjzz" : "xqykjzz";
          let data = transformData(
            currentZeroInfo.cwbbInfo,
            inputData[insType][type],
            type,
            insType
          );
          let ngWindows =
            $("#frmMain").length &&
            $("#frmMain").contents().find("#frmSheet")[0].contentWindow;
          let angular = ngWindows.angular;
          let controllerScope = angular.element("#viewCtrlId").scope();
          if (type === "xjllb") {
            if (insType === "qykjzz") {
              // 判断是work传过来的是否有值
              let isWorkValue = data.find((item) => {
                return parseFloat(item.bqje) || parseFloat(item.sqje1);
              });
              if (isWorkValue) {
                controllerScope.ybqyxjllbGrid.ybqyxjllbGridlb.forEach(
                  (item, index) => {
                    if (data[index]) {
                      item.bqje = data[index].bqje;
                      item.sqje1 = data[index].sqje1;
                    }
                  }
                );
              }
            } else if (insType === "xqykjzz") {
              // 判断是work传过来的是否有值
              let isWorkValue = data.find((item) => {
                return parseFloat(item.bnljje) || parseFloat(item.byje);
              });
              if (isWorkValue) {
                controllerScope.xqyxjllbGrid.xqyxjllbGridlb.forEach(
                  (item, index) => {
                    if (data[index]) {
                      item.byje = data[index].byje;
                      item.bnljje = data[index].bnljje;
                    }
                  }
                );
              }
            }
          }
          if (type === "lrb") {
            if (insType === "qykjzz") {
              // 判断是work传过来的是否有值
              let isWorkValue = data.find((item) => {
                return parseFloat(item.bqje) || parseFloat(item.sqje1);
              });
              if (isWorkValue) {
                controllerScope.ybqylrbGrid.ybqylrbGridlb.forEach(
                  (item, index) => {
                    if (data[index]) {
                      item.bqje = data[index].bqje;
                      item.sqje1 = data[index].sqje1;
                    }
                  }
                );
              }
            } else if (insType === "xqykjzz") {
              // 判断是work传过来的是否有值
              let isWorkValue = data.find((item) => {
                return parseFloat(item.byje) || parseFloat(item.bnljje);
              });
              if (isWorkValue) {
                controllerScope.syxqylrbGrid.syxqylrbGridlb.forEach(
                  (item, index) => {
                    if (data[index]) {
                      item.byje = data[index].byje;
                      item.bnljje = data[index].bnljje;
                    }
                  }
                );
              }
            }
          }
          if (type === "zcfzb") {
            // 判断是work传过来的是否有值
            let isWorkValue = data.find((item) => {
              return (
                parseFloat(item.qmyeZc) ||
                parseFloat(item.ncyeQy) ||
                parseFloat(item.ncyeQy) ||
                parseFloat(item.ncyeZc)
              );
            });
            if (insType === "qykjzz" && isWorkValue) {
              controllerScope.ybqyzcfzbzbGrid.ybqyzcfzbzbGridlb.forEach(
                (item, index) => {
                  if (data[index]) {
                    if (item.hasOwnProperty("qmyeZc")) {
                      item.qmyeZc = data[index].qmyeZc;
                    }
                    if (item.hasOwnProperty("ncyeQy")) {
                      item.ncyeQy = data[index].ncyeQy;
                    }
                    if (item.hasOwnProperty("qmyeQy")) {
                      item.qmyeQy = data[index].qmyeQy;
                    }
                    if (item.hasOwnProperty("ncyeZc")) {
                      item.ncyeZc = data[index].ncyeZc;
                    }
                  }
                }
              );
            } else if (insType === "xqykjzz" && isWorkValue) {
              controllerScope.syxqyzcfzbGrid.xqyzcfzbGridlb.forEach(
                (item, index) => {
                  if (data[index]) {
                    if (item.hasOwnProperty("qmyeZc")) {
                      item.qmyeZc = data[index].qmyeZc;
                    }
                    if (item.hasOwnProperty("ncyeQy")) {
                      item.ncyeQy = data[index].ncyeQy;
                    }
                    if (item.hasOwnProperty("qmyeQy")) {
                      item.qmyeQy = data[index].qmyeQy;
                    }
                    if (item.hasOwnProperty("ncyeZc")) {
                      item.ncyeZc = data[index].ncyeZc;
                    }
                  }
                }
              );
            }
          }
          angular.element("#viewCtrlId").scope().$apply();
          $("#btnSaved").click();
          nextTick(() => {
            if (
              document.querySelector(
                ".layui-layer-dialog > div.layui-layer-btn.layui-layer-btn- > a"
              )
            ) {
              document
                .querySelector(
                  ".layui-layer-dialog > div.layui-layer-btn.layui-layer-btn- > a"
                )
                .click();
              resolve(true);
              return true;
            }
          }, 500);
          return true;
        }, 500);
      };
      return true;
    }, 1000);
  });
};
const handleStep11 = async (data) => {
  currentZeroInfo = data;
  let timer = setInterval(async () => {
    if (
      $("#frmMain").length &&
      $("#frmMain").contents().find("#frmSheet").length &&
      $("#frmMain").contents().find("#divSheetlist > li").length
    ) {
      clearInterval(timer);
      timer = null;
      let frmMain = $("#frmMain").contents();
      let lis = Array.from(frmMain.find("#divSheetlist > li"));
      let xjllbBtn = lis.find((item) => $(item).text().includes("现金流量表"));
      let lrbBtn = lis.find((item) => $(item).text().includes("利润表"));
      let zcfzbBtn = lis.find((item) => $(item).text() === "资产负债表");
      await xjllbReport(xjllbBtn, "xjllb");
      await xjllbReport(lrbBtn, "lrb");
      await xjllbReport(zcfzbBtn, "zcfzb");
      if ($("#btnPrepareMake")) $("#btnPrepareMake").click();
      nextTick(() => {
        if (
          $("#frmMain")
            .contents()
            .find(
              "#layui-layer1 > div.layui-layer-btn.layui-layer-btn- > a.layui-layer-btn0"
            ).length
        ) {
          $("#frmMain")
            .contents()
            .find(
              "#layui-layer1 > div.layui-layer-btn.layui-layer-btn- > a.layui-layer-btn0"
            )[0]
            .click();
          nextTick(() => {
            if (
              document.querySelector(
                ".layui-layer-dialog > div.layui-layer-btn.layui-layer-btn- > a.layui-layer-btn0"
              )
            ) {
              document
                .querySelector(
                  ".layui-layer-dialog > div.layui-layer-btn.layui-layer-btn- > a.layui-layer-btn0"
                )
                .click();
              handleStep12();
              return true;
            }
          }, 500);
          return true;
        }
      }, 500);
    }
  }, 200);
};
const handleStep12 = async () => {
  nextTick(() => {
    window.sessionStorage.setItem("isCwbb", true);
    let success = $("#frmMain").contents().find("body > div > h3.font-green");
    if (success && success.text() === "申报成功") {
      let cwbb = currentZeroInfo.recordList.find((item) =>
        item.declareName.includes("财务报表")
      );
      cwbb.isSuccess = true;
      fixZeroInfo({ recordList: [qysds] });
      setTimeout(() => {
        window.location.href = "/xxmh/html/index_login.html";
      }, 1000);
    } else window.location.reload();
  }, 5000);
};

export {
  handleStep10,
  handleStep11,
  handleStep12,
  cwbbIsReport,
  cwbbReportStart,
};
